package mvp.com.bbc.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import mvp.com.bbc.R;
import mvp.com.bbc.appContact.AppContact;
import mvp.com.bbc.bean.ArtBeans;
import mvp.com.bbc.di.component.DaggerArtListComponent;
import mvp.com.bbc.di.modules.ArtListModules;
import mvp.com.bbc.ui.presenter.ArtListPresenter;

/**
 * Created by ${cbf} on 2017/10/12.
 */

public class TestFragmentReVh extends ArtListFragment2<TestFragmentReVh.Vholder> implements AppContact.ArtListView {

    private int mTotalPage;
    private boolean isPullDwonToRefersh;
    private List<ArtBeans.ArtBean> mArtBeen = new ArrayList<>();
    private ArtListPresenter mPresenter;
    private int pager=1;


    @Override
    protected TestFragmentReVh.Vholder initHolderView(ViewGroup parent) {
        View view = mInflater.inflate(R.layout.fragment_item_art, parent, false);
        return new Vholder(view);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    protected void initData(int pager) {
        if (pager == 0) {
            isPullDwonToRefersh = true;
        } else {

            isPullDwonToRefersh = false;
        }
       Log.v("cbf","pager = "+pager);
        mPresenter = DaggerArtListComponent
                .builder()
                .artListModules(new ArtListModules(this))
                .build()
                .getPresenter();
        mPresenter.artListPresenter("1", pager + "", "80");
        update(mArtBeen);
    }

    @Override
    protected void initHolderSetListener(TestFragmentReVh.Vholder holder, int layoutPosition) {

    }

    @Override
    protected void initHolderData(TestFragmentReVh.Vholder holder, int position) {
        if (mArtBeen != null && mArtBeen.size() > 0) {
            ArtBeans.ArtBean artBean = mArtBeen.get(position);
            Glide.with(getContext()).load(artBean.getIcon()).error(R.mipmap.ic_launcher).into(holder.mIvHead);
            holder.mTvCount.setText(artBean.getLn() + "");
            holder.mTvName.setText(artBean.getName());
            holder.mTvTime.setText(artBean.getCreattime().getTime());
        }
    }

    //回调数据
    @Override
    public void artListView(ArtBeans artBeans) {
        mTotalPage = artBeans.getTotalPage();
        List<ArtBeans.ArtBean> art = artBeans.getArt();
        if (isPullDwonToRefersh) {
            mArtBeen.clear();
        }
        if (art != null && art.size() > 0) {
            mArtBeen.addAll(art);
        } else {
            //等于空 或者没有数据的时候
        }
        update(mArtBeen);
    }

    public class Vholder extends RecyclerView.ViewHolder {

        private final ImageView mIvHead;
        private final TextView mTvTime;
        private final TextView mTvName;
        private final TextView mTvCount;

        public Vholder(View itemView) {
            super(itemView);
            mIvHead = (ImageView) itemView.findViewById(R.id.iv_head_art);
            mTvName = (TextView) itemView.findViewById(R.id.tv_name_art);
            mTvTime = (TextView) itemView.findViewById(R.id.tv_time_art);
            mTvCount = (TextView) itemView.findViewById(R.id.tv_art_click_count);
        }
    }
}
